/*
 * GlobalMem.cpp
 *
 *  Created on: 2012-9-1
 *      Author: yangentao@gmail.com
 */

#include "GlobalMem.h"
#include <assert.h>
namespace yet{
GlobalMem::GlobalMem()
		: hand(0), ptr(0) {
}
GlobalMem::GlobalMem(HGLOBAL hand_)
		: hand(hand_), ptr(0) {
	if (hand) {
		ptr = ::GlobalLock(hand);
	}
}
GlobalMem::~GlobalMem() {
	assert(!ptr);
}
void GlobalMem::Allocate(size_t bytes) {
	hand = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, bytes);
	if (hand) {
		ptr = ::GlobalLock(hand);
	}
}
HGLOBAL GlobalMem::Unlock() {
	assert(ptr);
	HGLOBAL handCopy = hand;
	::GlobalUnlock(hand);
	ptr = 0;
	hand = 0;
	return handCopy;
}
void GlobalMem::SetClip(UINT uFormat) {
	::SetClipboardData(uFormat, Unlock());
}
GlobalMem::operator bool() const {
	return ptr != 0;
}
SIZE_T GlobalMem::Size() {
	return ::GlobalSize(hand);
}

}
